package cn.com.shopec.erp.provideinterface.commo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.log4j.Logger;

/**
 * 
 * @author yangpinggui 2010-06-01
 * 
 */
public class HTTPConnect {
    private static final Logger log = Logger.getLogger(HTTPConnect.class);
    private static final String URL_CONTENT_TYPE = "Content-Type";
    private static final String URL_CHARSET = "charset";
    private static final String URL_SOAPAction = "SOAPAction";

    private URL url = null;
    private String myURL = "";
    private InputStream inStream = null;
    private OutputStream outStream = null;
    private HttpURLConnection httpConn = null;
    private String myEncoding = "", myContentType = "";
    private String myRevEncoding = "", mySOAPAction = "";
    private int nTimeOut = 30 * 1000;

    public HTTPConnect(String strUrl) {
        this.myURL = strUrl;
        this.myEncoding = "GBK";
        this.myRevEncoding = "GBK";
        this.myContentType = "text/html";
    }

    public void setEncoding(String Encoding) {
        this.myEncoding = Encoding;
    }

    public String getEncoding() {
        return this.myEncoding;
    }

    public void setRevEncoding(String Encoding) {
        this.myRevEncoding = Encoding;
    }

    public String getMySOAPAction() {
        return this.mySOAPAction;
    }

    public void setMySOAPAction(String mySOAPAction) {
        this.mySOAPAction = mySOAPAction;
    }

    public String getRevEncoding() {
        return this.myRevEncoding;
    }

    public void setContentType(String strType) {
        this.myContentType = strType;
    }

    public String getContentType() {
        return this.myContentType;
    }

    public String request(String strMsg) throws Exception {

        int nByte = 0, i = 0;
        String strResult = "";
        byte[] btRecv = null;
        byte[] btSend = null;

        try {
            this.connect(this.myURL);

            this.outStream = this.httpConn.getOutputStream();

            btSend = strMsg.getBytes(this.myEncoding);
            this.outStream.write(btSend);

            this.httpConn.getOutputStream().flush();

            this.inStream = this.httpConn.getInputStream();

            i = 0;
            btRecv = new byte[1024 * 16];
            while (nByte != -1) {
                nByte = this.inStream.read();
                if (nByte != -1) {
                    btRecv[i++] = (byte) nByte;
                }
            }

            strResult = new String(btRecv, 0, i, this.myRevEncoding);
        } catch (Exception e) {
            log.error("HTTPConnect request Exception:", e);
            throw e;
        } finally {
            try {
                if (this.inStream != null) {
                    this.inStream.close();
                }

                if (this.outStream != null) {
                    this.outStream.close();
                }

                if (this.httpConn != null) {
                    this.httpConn.disconnect();
                }
            } catch (Exception e) {
                log.error("HTTPConnect request Exception:", e);
            }

        }

        this.inStream = null;
        this.outStream = null;
        this.httpConn = null;

        return strResult;
    }

    /**
     * 
     * @param serverName
     * @throws IOException
     */
    public void connect(String serverName) throws IOException {

        this.url = new java.net.URL(serverName);
        this.httpConn = (HttpURLConnection) this.url.openConnection();
        this.httpConn.setDoOutput(true);
        this.httpConn.setDoInput(true);
        this.httpConn.setReadTimeout(this.nTimeOut);
        this.httpConn.setRequestProperty(URL_SOAPAction, this.mySOAPAction);
        this.httpConn.setRequestProperty(URL_CONTENT_TYPE, this.myContentType);
        this.httpConn.setRequestProperty(URL_CHARSET, this.myEncoding);

        return;
    }

    public void disConnect() throws IOException {
        this.httpConn.disconnect();

    }

}
